﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Turtle8
{
    public partial class TahasukaAlalCard : UserControl
    {
        static readonly public string TableName = "TahasukaAlal";
        TahasukaCard tCard;
        DataTable dt = new DataTable();
        SqlDataAdapter da;

        public SqlDataAdapter SqlDataAdapter
        { 
            get{ return da;}
            set{ da = value;}
        }

        public TahasukaAlalCard(TahasukaCard card)
        {
            InitializeComponent();
            this.Dock = DockStyle.Fill;

            tCard = card;

            string selectStr = CompanyManager.getSqlStringFromFile(@"\SELECT\TahasukaAlal.SELECT.sql");
            string updateStr = CompanyManager.getSqlStringFromFile(@"\UPDATE\TahasukaAlal.UPDATE.sql");
            string deleteStr = "DELETE FROM [TahasukaAlal] WHERE (armyID=@armyID AND startDate=@startDate)";
            string insertStr = CompanyManager.getSqlStringFromFile(@"\INSERT\TahasukaAlal.INSERT.sql");


            da = SqlManager.SqlDataAdapterFactory(selectStr);
            da.UpdateCommand = SqlManager.SqlCommandFactory(updateStr);
            da.DeleteCommand = SqlManager.SqlCommandFactory(deleteStr);
            da.InsertCommand = SqlManager.SqlCommandFactory(insertStr);

            da.SelectCommand.Parameters.Add("@endDate", SqlDbType.Date).Value = tCard.EndDate;
            ICollection<string> sqlParameterCollection = CompanyManager.parseSqlQueryParameters(updateStr);
            sqlParameterCollection.Distinct<string>();

            //adding parameters dynamically to the UPDATE SqlCommand
            foreach (string item in sqlParameterCollection)
            {
                string sItem = "@" + item;
                switch (item)
                {

                    case "armyID": 
                        da.UpdateCommand.Parameters.Add(CompanyManager.createSqlParameter(sItem, SqlDbType.NVarChar, DataRowVersion.Original, "armyID"));
                        da.InsertCommand.Parameters.Add(CompanyManager.createSqlParameter(sItem, SqlDbType.NVarChar, DataRowVersion.Original, "armyID"));
                        da.DeleteCommand.Parameters.Add(CompanyManager.createSqlParameter(sItem, SqlDbType.NVarChar, DataRowVersion.Original, "armyID"));

                        break;

                    case "startDate":
                        da.SelectCommand.Parameters.Add(sItem, SqlDbType.Date).Value = tCard.StartDate.Date;
                        da.UpdateCommand.Parameters.Add(sItem, SqlDbType.Date).Value = tCard.StartDate.Date;
                        da.InsertCommand.Parameters.Add(sItem, SqlDbType.Date).Value = tCard.StartDate.Date;
                        da.DeleteCommand.Parameters.Add(sItem, SqlDbType.Date).Value = tCard.StartDate.Date;
                        break;

                    default: 
                        da.UpdateCommand.Parameters.Add(CompanyManager.createSqlParameter("@" + item, SqlDbType.Bit, DataRowVersion.Current, item)).Value = false;
                        da.InsertCommand.Parameters.Add(CompanyManager.createSqlParameter("@" + item, SqlDbType.Bit, DataRowVersion.Current, item)).Value = false;

                        break;

                }
            }



            //Populate the DataTable by running the Fill method of the SqlDataAdapter.
            da.Fill(dt);
            dataGridView.DataSource = dt;
            CompanyManager.setTableColumnHeadersHebrew(dataGridView.Columns);
            refresh();
        }

        public void refresh() 
        {
            tCard.ArmyIDList.Clear();
            dt.Clear();
            da.Fill(dt);
            dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

            dataGridView.Columns["firstName"].ReadOnly = true;
            dataGridView.Columns["lastName"].ReadOnly = true;
            dataGridView.Columns["armyID"].ReadOnly = true;
            dataGridView.Columns["startDate"].Visible = false;
        }
 
        private void buttonUpdate_Click(object sender, EventArgs e)
        {
           da.Update(dt);
           string updateStr = @"UPDATE [QualifiedTrack] SET col_name=@col_val WHERE (col_name<@col_val OR col_name IS NULL) AND armyID=@armyID" ;

           for (int i = 0; i < dt.Rows.Count; i++)
            for (int j = 6; j < dt.Columns.Count; j++)
                if ((bool)dt.Rows[i][dt.Columns[j].ColumnName])
                    updateQualifiedTrackDBTable(updateStr, dt.Columns[j].ColumnName, (string)dt.Rows[i]["armyID"]);

              /*  if ((bool)dt.Rows[i]["nightShoot"] == true)
                    updateQualifiedTrackDBTable(updateStr, "nightShoot", (string)dt.Rows[i]["armyID"]);

                if ((bool)dt.Rows[i]["grenadeOperation"] == true)
                    updateQualifiedTrackDBTable(updateStr, "grenadeOperation", (string)dt.Rows[i]["armyID"]);

                if ((bool)dt.Rows[i]["pakalRefresh"] == true)
                    updateQualifiedTrackDBTable(updateStr, "pakalRefresh", (string)dt.Rows[i]["armyID"]);

                if ((bool)dt.Rows[i]["targilSufa"] == true)
                    updateQualifiedTrackDBTable(updateStr, "targilSufa", (string)dt.Rows[i]["armyID"]);

                if ((bool)dt.Rows[i]["firstAid"] == true)
                    updateQualifiedTrackDBTable(updateStr, "firstAid", (string)dt.Rows[i]["armyID"]);

                if ((bool)dt.Rows[i]["abcAttack"] == true)
                    updateQualifiedTrackDBTable(updateStr, "abcAttack", (string)dt.Rows[i]["armyID"]);*/
            

            labelSuccessfulUpdate.Visible = true;
            labelSuccessfulUpdate.Location = new Point(splitContainer.Panel1.Bounds.Width / 2, 16);
            timer.Interval = 5 * 1000;
            timer.Start();
        }

        private void updateQualifiedTrackDBTable(string updateStr, string col_name, string armyID)
        {
            SqlCommand updateCmd = SqlManager.SqlCommandFactory(updateStr);
            updateCmd.CommandText = updateCmd.CommandText.Replace("col_name", col_name);
            updateCmd.Parameters.Add("@col_val", SqlDbType.Date).Value = tCard.StartDate;
            updateCmd.Parameters.Add("@armyID", SqlDbType.NVarChar).Value = armyID;

            updateCmd.ExecuteNonQuery();
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            labelSuccessfulUpdate.Visible = false;
        }

    }
}
